package com.xiaocz.hblsubstitutedriving.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.base.BaseActivity;
import com.xiaocz.hblsubstitutedriving.model.UserInfo;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Author:  xiaocz.
 * Descr:  我的账户信息
 */
public class PersonalDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_work)
    TextView tvWork;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_expire_time)
    TextView tv_expire_time;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_sg_name)
    TextView tvsgName;

    public static void show(Context context) {
        Intent intent = new Intent(context, PersonalDetailsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_personal_details;
    }

    @Override
    protected void initData() {
        webGetUserInfo();
        super.initData();
    }

    @OnClick({R.id.btn_phone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_phone:
                callPhone(tvsgName.getHint().toString());
                break;
        }
    }

    /**
     * 获取用户信息
     */
    private void webGetUserInfo() {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        okHttpClient.post(Common.Constant.API_IP + "api/Driver/getDriverinfo", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                onDismiss();
                App.getInstance().showMessage(getResources().getString(R.string.connection_error));
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<UserInfo>>() {
                }.getType();
                RspModel<UserInfo> rspModel = getAnalysis(response, type, "获取用户信息");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.isStatus() && rspModel.getData() != null && !"".equals(rspModel.getData().toString())) {//保存用户
                            AccountManagers.login(rspModel.getData().getId(), rspModel.getData().getPhone(), new Gson().toJson(rspModel.getData()));
                            UserInfo info = rspModel.getData();
                            if (info != null) {
                                tvName.setText(info.getTruename());
                                tvCreateTime.setText(info.getCreatetime());
                                tvCity.setText(info.getCity_name());
                                tvId.setText(info.getIdnum());
                                tvSex.setText(info.getSex());
                                tv_expire_time.setText(String.valueOf("会员到期时间:" + info.getExpiretime()));
                                tvsgName.setText(info.getSiguan_name());
                                tvsgName.setHint(info.getSiguan_phone());
                                switch (info.getAuthentication()) {
                                    case 0://0-未认证 1-已认证 2-待认证
                                        tvWork.setText("未认证");
                                        break;
                                    case 1:
                                        tvWork.setText("已认证");
                                        break;
                                    case 2:
                                        tvWork.setText("待认证");
                                        break;
                                }
                            }
                        } else {
                            App.getInstance().showMessage(rspModel.getMessage());
                        }
                        break;
                    default:
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }
}
